local jiuling = fk.CreateSkill{
  name = "wk_heg__jiuling",
}

Fk:loadTranslationTable{
  ["wk_heg__jiuling"] = "柩灵",
  [":wk_heg__jiuling"] = "其他角色死亡时，你可移动其装备区内的一张牌，然后其可执行一次【英魂】的效果。",
  ["#wk_heg__jiuling-ask"] = "柩灵：选择一名角色，将此死亡角色装备区的一张牌移动至其装备区内",
  ["#wk_heg__jiuling_yinghun"] = "柩灵：你可对一名其他角色发动【英魂】",

  ["#jiuling_yinghun-draw"] = "摸已损失体力值数张牌，弃置一张牌",
  ["#jiuling_yinghun-discard"] = "弃置已损失体力值数张牌，摸一张牌",
}

jiuling:addEffect(fk.Death, {
  anim_type = "special",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and #target:getCardIds("e") > 0 and 
      table.find(player.room.alive_players, function(p) return target:canMoveCardsInBoardTo(p, "e") end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function (p) return target:canMoveCardsInBoardTo(p, "e") end)
    local tos = room:askToChoosePlayers(player, {
        targets = targets,
        min_num = 1,
        max_num = 1,
        prompt = "#wk_heg__jiuling-ask",
        skillName = jiuling.name,
        cancelable = true,
    })
    if #tos > 0 then
      event:setCostData(self, {tos = tos[1]})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos
    room:askForMoveCardInBoard(player, target, to, jiuling.name, "e", target)
    
    local n = target:getLostHp()
    local tos = player.room:askToChoosePlayers(target, {
        targets = room.alive_players,
        min_num = 1,
        max_num = 1,
        prompt = "#wk_heg__jiuling_yinghun",
        skillName = jiuling.name,
        cancelable = true
    })
    if #tos > 0 then
      local to2 = tos[1]
      local choice = room:askToChoice(target, {choices = {"#jiuling_yinghun-draw",  "#jiuling_yinghun-discard"}, skill_name = jiuling.name})
      if choice == "#jiuling_yinghun-draw" then
        to2:drawCards(n, jiuling.name)
        room:askToDiscard(to2, {
            min_num = 1,
            max_num = 1,
            include_equip = true,
            skillName = jiuling.name,
            cancelable = false,
        })
      else
        to2:drawCards(1, jiuling.name)
        room:askToDiscard(to2, {
            min_num = n,
            max_num = n,
            include_equip = true,
            skillName = jiuling.name,
            cancelable = false,
        })
      end
    end
  end,
})

return jiuling